
from copy import deepcopy

import past
from flask import Blueprint, request, jsonify
from controllers.common import *
from controllers.db import *

new_evip_shop_item_api = Blueprint('new_evip_shop_item_api', __name__)
new_evip_shop_item_down_api = Blueprint('new_evip_shop_item_down_api', __name__)

@new_evip_shop_item_api.route('/new_evip_shop_item_action', methods=['GET', 'POST'])
def new_evip_shop_item_action():
  params = json.loads(request.data)

  _action = params['action']
  methods = {
    'add_data': addMethod,
    'delete_data': deleteMethod,
    'clone_data': cloneMethod,
    'update_method': updateMethod,
    'get_item_method': getItemMethod,
    'get_list_method': getListMethod,
  }
  action = methods.get(_action, nullMethod)
  # 获取并执行策略、执行返回值code为0则表示正常
  result = action(params)

  if result['code'] != 0:
    return {'status': result[code], 'info': result[info]}
  result_data = {'status': 200, 'data': result['data'], 'info': 'request success!'}
  return jsonify(result_data)

@new_evip_shop_item_down_api.route('/new_evip_shop_item_down_action/<int:is_push>', methods=['GET', 'POST'])
def new_evip_shop_item_down_action(is_push):
  params = {
    "is_push": is_push
  }

  _action = 'push_issue'
  methods = {
    'create_json': createJson,
    'push_issue': pushIssue,
  }
  # 获取并执行策略aciotn
  action = methods.get(_action, nullMethod)
  result = action(params)

  # 直接返回push结果、不用特殊额外的处理
  return result


def nullMethod(param):
  return {'code': -1, 'data': '', 'info': '空方法'}


def addMethod(param):
  return {'code': -1, 'data': '', 'info': '方法未实现'}
  

def deleteMethod(param):
  dbObj = NewEvipShopItem.query.filter_by(project=param['project']).first()
  db.session.delete(dbObj)
  db.session.commit()
  projectData = NewEvipShopItem.query.filter_by(project=param['project']).first()
  if projectData is not None:
    return {'code': -2, 'data': '', 'info': 'projectData is not None,删除未成功'}
  else:
    return {'code': 0, 'data': '',  'info': '删除成功'}

def cloneMethod(param):
  data = NewEvipShopItem.query.filter_by(project=param['project']).first()
  cloneObj = NewEvipShopItem(
		remark = data.remark,
		basic = data.basic,

    )
  db.session.add(cloneObj)
  db.session.commit()
  return {'code': 0, 'data': '', 'info': 'clone success'}


def updateMethod(param):
  project = param['project']
  for name in param['data']:
    item = param['data']
    if name == 'remark':
      NewEvipShopItem.query.filter_by(project=project).update({
        'remark' : json.dumps(item['remark'])
        })
      db.session.commit()
    if name == 'basic':
      NewEvipShopItem.query.filter_by(project=project).update({
        'basic' : json.dumps(item['basic'])
        })
      db.session.commit()

  return {'code': 0, 'data': '', 'info': 'update success'}

def updateListMethod(param):
  pass

def getItemMethod(param):
  que = NewEvipShopItem.query.filter_by(project=param['project']).first()
  if que is None:
    return -2 
#  doc = get_operete_event_doc('ghost_town_event')

  tmp = {
    "project" : int(que.project),
	"remark" : json.loads(que.remark),
	"basic" : json.loads(que.basic)
  }
  return {'code': 0, 'data': tmp, 'info': 'update success'}

def getListMethod(param):
  data = NewEvipShopItem.query.order_by("project").all()
  result = [] 

  for que in data:
    tmp = {
      "project" : int(que.project),
	  "remark" : json.loads(que.remark),
	  "basic" : json.loads(que.basic),
    }
    result.append(tmp)
  return {'code': 0, 'data': result, 'info': 'update success'}

def politic(_action):
  methods={
    'add_data': addMethod,
    'delete_data': deleteMethod,
    'clone_data': cloneMethod,
    'update_method': updateMethod,
    'update_list_method': updateListMethod,
    'get_item_method': getItemMethod,
    'get_list_method': getListMethod,
  }
  return methods.get(_action, nullMethod)


def createJson(param):
  pass

def pushIssue(param):
  is_push = param['is_push']
  list = NewEvipShopItem.query.order_by("project").all()
  projs = {}
  # for x in range(len(post_data)):
  for que in list:
    basic = json.loads(que.basic)
    index = -1
    for good in basic:
      index+=1
      good['id'] = str(good['a'][0])+'_'+str(good['a'][1]) + '_'+str(good['a'][2])+'_'+str(index)
    tmp = {
      "goods" : basic
    }
    projs[que.project] = tmp
  return DownLoad('evip_item_tool_shop_proj', projs, is_push)

